﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iS3.Server.MetaConfig.Models
{
    //类型字典，方便输入
    public class DataTypeDict
    {
        private static Dictionary<string, DataType> CsharpDictReverse = new Dictionary<string, DataType>()
                {
                    {typeof(System.Int32).ToString()        , DataType.Csharp_Int32        },
                    {typeof(System.Single).ToString()        ,DataType.Csharp_Float           },
                    {typeof(System.Double).ToString()       , DataType.Csharp_Double           },
            { typeof(System.Decimal).ToString(),DataType.Csharp_Decimal },
                    {typeof(System.String).ToString()       , DataType.Csharp_String           },
                    {typeof(System.Boolean).ToString()       ,DataType.Csharp_Bool            },
                    {typeof(System.DateTime).ToString()      ,DataType.Csharp_DateTime         },
                    {typeof(System.Data.DataTable).ToString(),DataType.Csharp_DataTable        },
                };

        private static Dictionary<DataType, string> CsharpDict = new Dictionary<DataType, string>()
                {
                    { DataType.Csharp_Int32,        typeof(System.Int32).ToString()             },
                    { DataType.Csharp_Single,       typeof(System.Single).ToString()            },
                    { DataType.Csharp_Float,        typeof(System.Single).ToString()            },
                    { DataType.Csharp_Double,       typeof(System.Double).ToString()            },
                      { DataType.Csharp_Decimal,      typeof(System.Decimal).ToString()           },
                    { DataType.Csharp_String,       typeof(System.String).ToString()            },
                    { DataType.Csharp_Bool,         typeof(System.Boolean).ToString()           },
                    { DataType.Csharp_DateTime,     typeof(System.DateTime).ToString()          },
                    { DataType.Csharp_DataTable,    typeof(System.Data.DataTable).ToString()    },
                };
        private static Dictionary<DataType, Type> CsharpTypeDict = new Dictionary<DataType, Type>()
                {
                    { DataType.Csharp_Int32,        typeof(System.Int32)            },
                    { DataType.Csharp_Single,       typeof(System.Single)           },
                    { DataType.Csharp_Float,        typeof(System.Single)           },
                    { DataType.Csharp_Double,       typeof(System.Double)           },
                      { DataType.Csharp_Decimal,      typeof(System.Decimal)           },
                    { DataType.Csharp_String,       typeof(System.String)            },
                    { DataType.Csharp_Bool,         typeof(System.Boolean)          },
                    { DataType.Csharp_DateTime,     typeof(System.DateTime)      },
                    { DataType.Csharp_DataTable,    typeof(System.Data.DataTable)    },
                };
        public static DataType CsharpType2DataType(string CsharpType)
        {
            if (CsharpDictReverse.ContainsKey(CsharpType))
            {
                return CsharpDictReverse[CsharpType];
            }
            return DataType.Csharp_UnKnow;
        }
        public static Type DataType2CsharpType(string DataType)
        {
            DataType dType = (DataType)Enum.Parse(typeof(DataType), DataType);
            if (CsharpTypeDict.ContainsKey(dType))
            {
                return CsharpTypeDict[dType];
            }
            return null;
        }
        public static string GetCsharpType(string EnumStr)
        {
            try
            {
                List<string> EnumList = Enum.GetNames(typeof(DataType)).ToList();

                int index = EnumList.IndexOf(EnumStr);
                if (index != -1)
                {
                    return CsharpDict.ElementAt(index).Value;
                }
                else
                {
                    throw new Exception($"不存在数据类型{EnumStr}");
                }

            }
            catch (Exception ex)
            {
                throw new Exception($"数据类型{EnumStr}转换为Csharp类型报错", ex);
            }
        }






        private static Dictionary<DataType, string> SqlDict = new Dictionary<DataType, string>()
                {
                    { DataType.Csharp_Int32,        "[int]"               },
                    { DataType.Csharp_Single,       "[float]"             },
                    { DataType.Csharp_Float,        "[float]"             },
                    { DataType.Csharp_Double,       "[float]"             },
                    { DataType.Csharp_String,       "[nvarchar](4000)"    },
                    { DataType.Csharp_Bool,         "[bit]"               },
                    { DataType.Csharp_DateTime,     "[datetime]"         },
                    { DataType.Csharp_DataTable,    "[nvarchar](max)"     },
                };

        public static string GetSqlType(string EnumStr)
        {
            try
            {
                List<string> EnumList = Enum.GetNames(typeof(DataType)).ToList();

                int index = EnumList.IndexOf(EnumStr);
                if (index != -1)
                {
                    return SqlDict.ElementAt(index).Value;
                }
                else
                {
                    throw new Exception($"不存在数据类型{EnumStr}");
                }

            }
            catch (Exception ex)
            {
                throw new Exception($"数据类型{EnumStr}转换为Sql类型报错", ex);
            }
        }






    }
}
